package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.log.enums.OperatorType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@Tag(name = "品牌管理模块")
public class BrandController extends BaseController {


    @Resource
    BrandService brandService;

    //记录操作日志
    @Log(title = "条件查询品牌分页数据", businessType = BusinessType.OTHER, isSaveResponseData = true, isSaveRequestData = false)
    @Operation(summary = "条件查询品牌分页数据")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    @Log(title = "新增品牌", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            //品牌名称被占用
            return error("新增品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.insertBrand(brand));
    }


    @Log(title = "更新品牌", businessType = BusinessType.UPDATE)
    @Operation(summary = "更新品牌")
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            return error("更新品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateBrand(brand));
    }


    //    @Operation(summary = "删除品牌")
//    @DeleteMapping("{brandId}")
//    public AjaxResult  remove(@PathVariable Long brandId ){
//
//        return toAjax(brandService.removeById(brandId));
//
//    }
    @Log(title = "更新品牌", businessType = BusinessType.DELETE)
    @Operation(summary = "批量删除")
    @DeleteMapping("{brandIds}")
    public AjaxResult removeList(@PathVariable("brandIds") List<Long> brandIds) {
        return toAjax(brandService.removeByBrandId(brandIds));


    }

    @Log(title = "查询品牌详情", businessType = BusinessType.OTHER)
    @Operation(summary = "查询品牌详情")
    @GetMapping("{brandId}")
    public AjaxResult getById(@PathVariable Long brandId) {
        return success(brandService.getById(brandId));

    }


    @Operation(summary = "查询所有品牌")
    @GetMapping
    public AjaxResult listAll() {
        List<Brand> list = brandService.list();
        return success(list);
    }


    @InnerAuth
    @Operation(summary = "查询所有品牌")
    @GetMapping("listAllBrands")
    public R<List<Brand>> listAllBrands() {
        List<Brand> list = brandService.list();
        return R.ok(list );
    }

}
